package com.bsj.media.init.dto;

import com.bsj.consumerQ.until.ExceptionUntil;
import com.bsj.media.media.MediaSaveInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
public abstract class NetDto {

    Thread thread;

    /**
     * 用于限流控制
     */
    private Thread threadCheck;

    boolean flagCheck = false;

    boolean flagRun = true;

    String id;

    /**
     * 累计的I，P帧的个数
     */
    private volatile int anInt = 0;

    /**
     * 第一帧出现的时间
     */
    private long time;

    /**
     * 当前帧的时间(毫秒)
     */
    private long nowFTime;
    /**
     * 下载结束时间
     */
    private long endTime;

    private String sim;

    private String channel;

    protected int codeRateValue = 12;

    protected MediaSaveInfo mediaSaveInfo;

    private LinkedBlockingQueue<byte[]> linkedBlockingQueue = new LinkedBlockingQueue<>();

    public void push(byte[] data, long nowFTime) {
        try {
            linkedBlockingQueue.put(data);
            this.nowFTime = nowFTime;
        } catch (Exception e) {
            log.error("push传输NetDto异常:" + ExceptionUntil.getErrorStr(e));
        }
    }

    public void setMediaSaveInfo(MediaSaveInfo now) {
        if (now != null) {
            this.mediaSaveInfo = now;
            int codeRateValueNow = mediaSaveInfo.getCodeRateValue();
            if (codeRateValueNow > 0) {
                this.codeRateValue = codeRateValueNow;
            }
        }
    }

    public String getId() {
        return this.id;
    }

    public String getSim() {
        return this.sim;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public void init(String nowId) {
        this.id = nowId;
        String[] split = id.split("-");
        sim = split[0];
        if (split[0].length() >= 12) {
            sim = split[0].substring(1);
        }
        channel = split[1];
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flagRun) {
                    long millis = System.currentTimeMillis();
                    byte[] poll = linkedBlockingQueue.poll();
                    if (poll != null) {
                        sendData(poll);
                        time = millis;
                    }
                    if (time != 0L && millis - time > 3000) {
                        //3S没有数据,让发送那里判断是否文件下载，是文件下载直接关闭链路
                        downloadClose();
                    }
                    if (endTime != 0L && nowFTime >= endTime) {
                        log.info("解析时间一致，关闭下载nowFTime: {}, endTime:{}", nowFTime, endTime);
                        downloadClose();
                    }
                    //判断每帧的数据与
                    try {
                        //避免线程一直RUN，导致CPU过载
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        log.warn("推流线程被关闭: {}", e.getMessage());
                        Thread.currentThread().interrupt();
                    }
                }
            }
        });
        thread.start();
        log.info("传输NetDto:" + id);
    }

    public abstract void sendData(byte[] bytes);

    public abstract boolean headFlag();

    public abstract void downloadClose();


    public void stop() {
        if (thread != null) {
            flagRun = false;
            thread.stop();
        }
    }
}
